/*
	File: C:/works/3d/games/mgt/panzer_2010/data/scripts/procedural_walk.nut
	Author: Astrofra
*/

		Include("data/scripts/globals.nut")

/*!
	@short	ProceduralWalk
	@author	Astrofra
*/
class	ProceduralWalk
{
	body			=	0
	time			=	0
	master_phase	=	0
	master_sin		=	0
	master_cos		=	0
	torso_pos		=	0

	/*!
		@short	OnUpdate
		Called during the scene update, each frame.
	*/
	function	OnUpdate(item)
	{
		time = g_clock / 1000.0 / 10.0 * 60.0 * 10.0
		master_phase = Deg(Mod(time, 360.0))
		master_sin = sin(master_phase)
		print("time         = " + time)
		print("master_phase = " + master_phase)

		//	Body
		ItemSetPosition(body.torso, torso_pos + Vector(0, 0.05 * sin2(master_phase * 2.0), 0))
		ItemSetRotation(body.torso, Vector(Deg(5.0), Deg(180.0), Deg(-5.0 * sin2(master_phase * 1.0))))

		//	Head
		ItemSetRotation(body.head, Vector(0, Deg(5.0 * sin2(master_phase)), 0))

		//	Arms
		ItemSetRotation(body.arm_r, Vector(Deg(10.0) + Deg(55.0 * sin2(master_phase)) ,0,0))
		ItemSetRotation(body.forearm_r, Vector(Deg(-5.0) - Deg(30.0 * (1.0 + sin2(master_phase + Deg(45.0)) ) ) ,0,0))

		ItemSetRotation(body.arm_l, Vector(Deg(10.0) + Deg(55.0 * sin2(master_phase + Deg(180.0))) ,0,0))
		ItemSetRotation(body.forearm_l, Vector(Deg(-5.0) - Deg(30.0 * (1.0 + sin2(master_phase + Deg(180.0) + Deg(45.0)) ) ) ,0,0))

		//	Legs
		ItemSetRotation(body.leg_l, Vector(Deg(45.0 * sin2(master_phase)) ,0,0))
		ItemSetRotation(body.ankle_l, Vector(Deg(45.0 * (1.0 + sin2(master_phase + Deg(90.0))) ) ,0,0))
		ItemSetRotation(body.foot_l, Vector(Deg(25.0 * sin2(master_phase + Deg(45.0))) ,0,0))
		ItemSetRotation(body.toe_l, Vector(Deg(-25.0 * (1.0 + sin2(master_phase + Deg(-15.0))) ) ,0,0))

		ItemSetRotation(body.leg_r, Vector(Deg(45.0 * sin2(master_phase + Deg(180.0))) ,0,0))
		ItemSetRotation(body.ankle_r, Vector(Deg(45.0 * (1.0 + sin2(master_phase + Deg(180.0) + Deg(90.0))) ) ,0,0))
		ItemSetRotation(body.foot_r, Vector(Deg(25.0 * sin2(master_phase + Deg(180.0) + Deg(45.0))) ,0,0))
		ItemSetRotation(body.toe_r, Vector(Deg(-25.0 * (1.0 + sin2(master_phase + Deg(180.0) + Deg(-15.0))) ) ,0,0))
	}

	/*!
		@short	OnSetup
		Called when the item is about to be setup.
	*/
	function	OnSetup(item)
	{
		body = {
			root = 0,
			torso = 0,
			head = 0,

			arm_l = 0,
			forearm_l = 0,

			arm_r = 0,
			forearm_r = 0,

			leg_l = 0,
			ankle_l = 0,
			foot_l = 0,
			toe_l = 0,

			leg_r = 0,
			ankle_r = 0,
			foot_r = 0,
			toe_r = 0
		}

		body.root = item
		body.torso = ItemGetChild(item, "torso")
		body.head = ItemGetChild(body.torso, "head")

		body.arm_l = ItemGetChild(body.torso, "arm_l")
		body.forearm_l = ItemGetChild(body.arm_l, "forearm_l")

		body.arm_r = ItemGetChild(body.torso, "arm_r")
		body.forearm_r = ItemGetChild(body.arm_r, "forearm_r")

		body.leg_l = ItemGetChild(body.torso, "leg_l")
		body.ankle_l = ItemGetChild(body.leg_l, "ankle_l")
		body.foot_l = ItemGetChild(body.ankle_l, "foot_l")
		body.toe_l = ItemGetChild(body.foot_l, "toe_l")

		body.leg_r = ItemGetChild(body.torso, "leg_r")
		body.ankle_r = ItemGetChild(body.leg_r, "ankle_r")
		body.foot_r = ItemGetChild(body.ankle_r, "foot_r")
		body.toe_r = ItemGetChild(body.foot_r, "toe_r")

		torso_pos = ItemGetPosition(body.torso)
	}
}
